var app = angular.module('app.controllers-hotel', []);


/**
 * 法人信息
 */
app.controller('hotelEgalPersonInfoCtrl', ['$scope', 'HotelService', '$cordovaToast', function ($scope, HotelService, $cordovaToast) {

  $scope.$on('$ionicView.beforeEnter', function () {

    $scope.editEnbable = false;
    $scope.hotel = {};

    $scope.hotelSeatCountErrorTips = {
      required: '请填写旅馆床位数',
      minlength: '床位数至少为1'
    };


  });
  $scope.$on('$ionicView.afterEnter', function () {
    HotelService.getHotelBasicInfo().then(function (data) {
      $scope.hotel = data.hotel;
      //alert(data.hotel.id);
    }, function (error) {
      alert(error.message);
    })
  });
  $scope.toggleEnableEdit = function () {
    $scope.editEnbable = !$scope.editEnbable;
  };

  $scope.submit = function () {

    HotelService.modifyLegalPerson($scope.hotel.legalpersonphonenum, $scope.hotel.legalpersonaddress).then(function () {
      $scope.editEnbable = false;
      $cordovaToast.showShortCenter('修改成功');
    }, function (error) {
      $scope.editEnbable = true;
      $cordovaToast.showShortCenter(error.message);
    });

  };


}]);

/**
 * 旅馆基本信息
 */
app.controller('HotelBasicInfoCtrl', ['$scope', 'HotelService', '$cordovaToast', 'AppSettings',
    function ($scope, HotelService, $cordovaToast, AppSettings) {

      $scope.$on('$ionicView.beforeEnter', function () {

        $scope.editEnbable = false;
        $scope.hotel = {};

        $scope.items = [];

        $scope.hotelSeatCountErrorTips = {
          required: '请填写旅馆床位数',
          minlength: '床位数至少为1'
        };


      });
      $scope.$on('$ionicView.afterEnter', function () {
        HotelService.getHotelBasicInfo().then(function (data) {
          $scope.hotel = data.hotel;
          for (var idx = 0; data.hotel && idx < data.hotel.avaiableResImages.length; idx++) {
            var image = $scope.hotel.avaiableResImages[idx];
            if (image && image.id) {
              var imageUrl = AppSettings.resEndpoint + "?imageId=" + image.id;
              $scope.items.push(imageUrl);
            }
          }

          //alert(data.hotel.id);
        }, function (error) {
          alert(error.message);
        })
      });
      $scope.toggleEnableEdit = function () {
        $scope.editEnbable = !$scope.editEnbable;
      };

      $scope.submit = function () {

        HotelService.modifyHotelSeatCount($scope.hotel.hotelseatcount).then(function () {
          $scope.editEnbable = false;
          $cordovaToast.showShortCenter('修改成功');
        }, function (error) {
          $scope.editEnbable = true;
          $cordovaToast.showShortCenter(error.message);
        });

      };


    }])

  .controller('HotelNotApprovedTipCtrl', ['$scope', '$rootScope', function ($scope, $rootScope) {
    $scope.$on('$ionicView.afterEnter', function () {
      if ($rootScope.hotelStatus == 'waiting') {
        $scope.tipImg = 'img/iconfont-lock.png';
        $scope.tipTitle = '您提交的资料正在审核中,请耐心等待...';
        $scope.isShowResubmitButton = false;
      } else {
        $scope.tipImg = 'img/iconfont-cry.png';
        $scope.tipTitle = '您提交的资料审核不通过';
        $scope.isShowResubmitButton = true;
        $scope.tipAduitSuggestion = $rootScope.aduitSuggestion;
      }
    });
  }])
  .controller('ResubmitMaterialCtrl', ['$scope', '$rootScope', 'HotelService', '$cordovaToast', '$state', 'Storage',
    function ($scope, $rootScope, HotelService, $cordovaToast, $state, Storage) {

      $scope.$on('$ionicView.beforeEnter', function () {
        //旅馆照片
        $scope.hotelImages = [];
        $scope.includeTmpls = [
          'templates/hotelRegisterLegalPerson.html',
          'templates/hotelRegisterLbasic.html',
        ];
      });
      /**
       * 先从服务器上获取旅馆信息
       */
      $scope.$on('$ionicView.afterEnter', function () {


        $scope.userNameErrorTips = {
          required: '用户名不能为空'
        };
        $scope.passwordErrorTips = {
          required: '密码不能为空'
        };
        $scope.confirmPasswordErrorTips = {
          required: '确认密码不能为空'
        };
        $scope.ordinaryAddressErrorTips = {
          required: '请填写现住地址'
        };

        $scope.ordinaryNameErrorTips = {
          required: '请填写姓名'
        };
        $scope.ordinaryidCardNoErrorTips = {
          required: '请填写身份证号码'
        };
        $scope.legalPersonNameErrorTips = {
          required: '请填写法人姓名'
        };

        $scope.legalPersonNameErrorTips = {
          required: '请填写法人姓名'
        };
        $scope.legalPersonGenderErrorTips = {
          required: '请选择法人性别'
        };
        $scope.legalPersonIDCardNoErrorTips = {
          required: '请填写法人身份证号码'
        };
        $scope.legalPersonAddressErrorTips = {
          required: '请填写法人现住地址'
        };

        $scope.hotelNameErrorTips = {
          required: '请填写旅馆名称'
        };
        $scope.hotelSeatCountErrorTips = {
          required: '请填写旅馆床位数'
        };

        $scope.hotelAddressErrorTips = {
          required: '请填写旅馆地址'
        };

        $scope.hotelGovernOfficeErrorTips = {
          required: '请填写所在的派出所'
        };

        HotelService.getHotelBasicInfo().then(function (data) {
          $scope.user = data.hotel;

          $scope.user.loginName = data.hotel.user.loginName;
          $scope.user.legalpersongender = '男' == data.hotel.user.legalpersongender ? '1' : '2';
        }, function (error) {
          $cordovaToast.showShortCenter(error.message);
        })
      });

      /**
       *  提交资料
       */
      $scope.submit = function () {

        /**
         * 组装要上传的图片
         */
        for (var idx = 0; idx < $scope.hotelImages.length; idx++) {
          var hotelImage = $scope.hotelImages[idx];
          var imageIdx = 'image' + (idx + 1);
          $scope.user[imageIdx] = hotelImage;

          $scope.user[imageIdx] = {};
          $scope.user[imageIdx].data = hotelImage;
        }

        HotelService.doResubmitHotelRegister($scope.user).then(function () {
          $cordovaToast.showShortCenter('提交成功,请耐心等待审核');
          //清理掉用户缓存的userToken认证信息
          Storage.clearUserToken();
          $rootScope.tipAduitSuggestion = null;


          //重新返回登陆界面
          $state.go('signIn', {}, {reload: true});

        }, function (error) {
          $cordovaToast.showShortCenter(error.message);
        });
      }

    }])

  /**
   * 添加入住人员
   */
  .controller('HotelAddGuestCtrl',
    ['$scope', '$state', '$ionicPopup', 'HotelService', '$cordovaToast',
      function ($scope, $state, $ionicPopup, HotelService, $cordovaToast) {

        $scope.$on('$ionicView.beforeEnter', function () {
          //表单验证提示
          $scope.guestNameErrorTips = {
            required: '入住人员姓名不能为空'
          };
          $scope.roomNoErrorTips = {
            required: '请填写房间名称或者编号'
          };

          $scope.guestIDErrorTips = {
            required: '入住身份证号码不能为空',
            maxlength: '二代身份证为18位',
            minlength: '二代身份证为18位'
          };

          $scope.guest = {};
        });

        $scope.$on('$ionicView.afterEnter', function () {

        });

        /**
         * 通过扫描获得入住人员信息
         */
        $scope.fillInGuestInfoByScan = function () {
          console.log("fillInGuestInfoByScan");
        };

        // A confirm dialog
        $scope.showConfirm = function () {
          var confirmPopup = $ionicPopup.confirm({
            title: '温馨提示',
            template: '添加成功,是否继续添加?'
          });

          confirmPopup.then(function (res) {
            if (res) {
              //reset form
              $scope.guest = {};
            } else {
              $state.go('hotelGuestList');
            }
          });
        };
        $scope.submit = function () {
          console.log($cordovaToast);
          //var idInfo = getIdCardInfo($scope.guest.idCardNo);
          //if (!idInfo || !idInfo.isTrue) {
          //  $cordovaToast.showShortCenter("请输入正确的身份证号码");
          //  return;
          //}

          HotelService.addGuest($scope.guest).then(function () {
            $scope.showConfirm();
          }, function (error) {
            $cordovaToast.showShortCenter(error.message);
          });

        };
      }])
  /**
   * 入住人员列表
   */
  .controller('HotelGuestListCtrl', ['$scope', '$ionicModal',
    '$cordovaDatePicker', 'HotelService', '$cordovaToast', 'CommonService',
    function ($scope, $ionicModal, $cordovaDatePicker, HotelService, $cordovaToast, CommonService) {
      $ionicModal.fromTemplateUrl('templates/modal/guestListSearchModal.html', {
        scope: $scope,
        animation: 'slide-in-up'
      }).then(function (modal) {
        $scope.modal = modal;
      });

      $scope.$on('$ionicView.afterEnter', function () {

      });
      $scope.$on('$ionicView.beforeEnter', function () {
        $scope.guests = [];
        $scope.searchCondition = {};
      });


      //选择开始时间
      $scope.inputQueryStartTime = function () {
        CommonService.getTimePicker().then(function (timeString, date) {
          $scope.searchCondition.beginCreateDate = timeString;

        });
      };
      $scope.inputQueryEndTime = function () {
        CommonService.getTimePicker().then(function (timeString, date) {
          $scope.searchCondition.endCreateDate = timeString;
        });
      };

      $scope.submit = function () {
        // alert(angular.toJson($scope.searchCondition));
        HotelService.getGuestList($scope.searchCondition).then(function (result) {
          $scope.searchCondition = {};
          $scope.guests = result.hotelGuestList;
          $scope.modal.hide();

        }, function (error) {
          $cordovaToast.showShortCenter(error.message);
        });
      }


    }])


  /**
   * 添加从业人员
   */
  .controller('HotelAddWorkerCtrl', ['$scope', '$ionicLoading', function ($scope, $ionicLoading) {

  }])

  .controller('hotelWorkerListCtrl', ['$scope', '$ionicModal', 'HotelService', '$cordovaToast', '$ionicListDelegate',
    function ($scope, $ionicModal, HotelService, $cordovaToast, $ionicListDelegate) {
      $scope.$on('$ionicView.beforeEnter', function () {

      });

      $scope.$on('$ionicView.afterEnter', function () {
        $ionicModal.fromTemplateUrl('templates/modal/addHotelWokerModal.html', {
          scope: $scope,
          animation: 'slide-in-up'
        }).then(function (modal) {
          $scope.modal = modal;
        });

        $scope.nameErrorTips = {
          required: '请填写从业人员姓名'
        };
        $scope.idCardNoErrorTips = {
          required: '请填写从业人员身份证',
          maxlength: '身份证为18位',
          minlength: '身份证为18位',
        };
        $scope.addressErrorTips = {
          required: '请正确认真填写现地址',
          maxlength: '请正确认真填写现地址',
          minlength: '请正确认真填写现地址',
        };
        $scope.sexs = [
          {
            "label": '男',
            "value": '1'
          }, {
            "label": '女',
            "value": '2'
          }
        ];
        $scope.data = {sex: '1'};

        $scope.workerList = [];

        $scope.remove = function (item) {
          HotelService.removeWorker(item).then(function (data) {
            $ionicListDelegate.closeOptionButtons();
            $cordovaToast.showShortCenter('删除成功');
            $scope.doRefresh();
          }, function (error) {
            $cordovaToast.showShortCenter(error.message);
          })
        }
        $scope.doRefresh = function () {
          HotelService.getWorkerList().then(function (data) {
            // Stop the ion-refresher from spinning
            $scope.$broadcast('scroll.refreshComplete');
            $scope.workerList = data.hotelWorkerList;
          }, function (error) {
            // Stop the ion-refresher from spinning
            $scope.$broadcast('scroll.refreshComplete');
            $cordovaToast.showShortCenter(error.message);
          })
        }
        $scope.submit = function () {

          var idInfo = getIdCardInfo($scope.data.idCardNo);
          if (!idInfo || !idInfo.isTrue) {
            $cordovaToast.showShortCenter("请输入正确的身份证号码");
            return;
          }

          HotelService.addWork($scope.data).then(function () {
            $scope.data = {sex: "1"};
            $cordovaToast.showShortCenter('提交成功');
          }, function (error) {
            $cordovaToast.showShortCenter(error.message);
          });
        }
        //手动刷新数据
        $scope.doRefresh();
      });

    }])
;

